package com.qf.controller;

import com.qf.entity.*;
import com.qf.feign.CityFeign;
import com.qf.feign.HotalFeign;
import com.qf.feign.RoomFeign;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private CityFeign cityFeign;
    @Autowired
    private HotalFeign hotalFeign;
    @Autowired
    private RoomFeign roomFeign;

    String uploadPath = "F:/Image";

    @RequestMapping("/citylist")
    public String cityList(Model model){
        R<List<City>> cityResult = cityFeign.list();
        if(cityResult.getCode() == R.Code.SUCCESS){
            model.addAttribute("cityList",cityResult.getData());
        }
        return "citylist";
    }
    @RequestMapping("/addcity")
    public String addList(City city){
        R<Boolean> addResult = cityFeign.insert(city);
        System.out.println(addResult);
        return "redirect:/system/citylist";
    }
    @RequestMapping("/cityListJson")
    @ResponseBody
    public List<City> cityListJson(){
        R<List<City>> cityResult = cityFeign.list();
        return cityResult.getData();
    }
    @RequestMapping("/hotaladd")
    public String insert(Hotal hotal,MultipartFile file){
        String outPath = uploadPath+"/"+ UUID.randomUUID().toString();
        try (
            InputStream is = file.getInputStream();
            OutputStream os = new FileOutputStream(outPath);
        ){
            IOUtils.copy(is,os);
        } catch (IOException e) {
            e.printStackTrace();
        }

        hotal.setHotalImage(outPath);
        R<Boolean> addResult = hotalFeign.insert(hotal);
        System.out.println(addResult);
        return "redirect:/system/hotalList";
    }
    @RequestMapping("/hotalList")
    public String hotalList(Model model){
        R<List<Hotal>> hotalList = hotalFeign.list();
        if(hotalList.getCode() == R.Code.SUCCESS){
            model.addAttribute("hotalList",hotalList.getData());
        }
        return "hotalList";
    }
    @RequestMapping("/CustomList")
    public String roomList(Integer hid,Model model){
        R<List<Room>> list = roomFeign.List(hid);
        if(list.getCode() == R.Code.SUCCESS){
            model.addAttribute("roomList",list.getData());
        }
        return "roomList";
    }
    @RequestMapping("/roomAdd")
    public String addRoom(Room room){
        R<Boolean> addResult = roomFeign.insert(room);
        System.out.println(addResult);
        return "redirect:/system/hotalList";
    }
    @RequestMapping("/roompriceList")
    public String toupdateprice(Integer rid, Model model){
        R<List<RoomPrice>> roomPrice = roomFeign.getRoomPrice(rid);
        model.addAttribute("roomPriceList", roomPrice.getData());
        return "roomPrice";
    }
    /**
     * 修改房间价格
     * @return
     */
    @RequestMapping("/updatepirce")
    public String updatepirce(RoomPrice roomPrice){
        roomFeign.updatePrice(roomPrice);
        return "redirect:/system/roompriceList?rid=" + roomPrice.getRid();
    }
    /**
     * 跳转到客房管理页面
     * @return
     */
    @RequestMapping("/toroomadd")
    public String toroomadd(){
        return "addRoom";
    }

    @RequestMapping("/dbExport")
    public String dbExport(Model model){
        R<List<Hotal>> hotalList = hotalFeign.list();
        if(hotalList.getCode() == R.Code.SUCCESS){
            model.addAttribute("hotalList",hotalList.getData());
        }
        return "dbExport";
    }

}
